package com.fanyouzhao.ehome.web.servlet; /**
 * @Author: fanyouzhao
 * @Date: 14:48 2021/5/14
 */

import com.alibaba.fastjson.JSON;
import com.fanyouzhao.ehome.entity.GetVo;
import com.fanyouzhao.ehome.service.GetVoService;
import com.fanyouzhao.ehome.service.impl.GetVoServiceImpl;
import com.fanyouzhao.ehome.util.JsonResult;
import com.fanyouzhao.ehome.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GetServlet", value = "/get")
public class GetServlet extends HttpServlet {
    private final GetVoService getVoService = new GetVoServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //中文处理
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        ((HttpServletResponse) response).setHeader("content-Type", "text/html;charset=UTF-8");

        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(null);

        //获取操作的类型 method
        String method = request.getHeader("method");
        switch (method) {
            case "selectByPage": {
                Integer page = Integer.parseInt(request.getParameter("page"));
                Integer limit = Integer.parseInt(request.getParameter("limit"));
                try {
                    List<GetVo> getVos = selectByPage(page, limit);
                    if (getVos != null) {
                        jsonResult = new JsonResult("0","查询成功",getVos);
                        jsonResult.setValue("count", getCount());
                    } else {
                        jsonResult = new JsonResult("1","查询失败！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case "search": {
                Integer page = Integer.parseInt(request.getParameter("page"));
                Integer limit = Integer.parseInt(request.getParameter("limit"));
                String searchString = request.getParameter("searchString");
                try {
                    List<GetVo> searchGetVos = search(searchString, page, limit);
                    if (searchGetVos != null) {
                        jsonResult = new JsonResult("0","查询成功",searchGetVos);
                        jsonResult.setValue("count",getCountSearch(searchString));
                    } else {
                        jsonResult = new JsonResult("1","查询失败！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            default:
                break;
        }
        ResponseUtil.send(response, JSON.toJSONString(jsonResult.getValues()));
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public List<GetVo> selectByPage(Integer page, Integer size) throws Exception {
        return getVoService.selectByPage(page, size);
    }

    public Integer getCount() throws Exception{
        return getVoService.getCount();
    }

    public Integer getCountSearch(String searchString) throws Exception{
        return getVoService.getCountSearch(searchString);
    }

    public List<GetVo> selectAll() throws Exception{
        return getVoService.selectAll();
    }
    public List<GetVo> search(String searchString, Integer page, Integer size) throws Exception{
        return getVoService.searchByPhoneorName(searchString,page,size);
    }
}
